package com.ifonly.activerecord;

import com.ifonly.common.typeconverter.TypeConvert;

import java.io.Serializable;
import java.util.Map;

/**
 * @author ifonly
 * @version 1.0 2016-01-22 15:17
 * @since JDK 1.6
 */
public class Accessor implements Access, Serializable {
    private static final long serialVersionUID = -8641331004600491455L;
    
    private Map<String, Object> map;

    public Accessor(Map<String, Object> map) {
        this.map = map;
    }

    @Override
    public Object get(String property) {
        return this.map.get(property);
    }

    @Override
    public Number getNumber(String property) {
        return (Number) get(property);
    }

    @Override
    public String getString(String property) {
        return getString(property, null);
    }

    @Override
    public String getString(String property, String defaultValue) {
        String str = TypeConvert.convert(String.class, get(property));
        return str == null ? defaultValue : str;
    }

    @Override
    public Long getLong(String property) {
        return TypeConvert.convert(Long.class, get(property));
    }

    @Override
    public long getLongValue(String property) {
        return getLongValue(property, 0);
    }

    public long getLongValue(String property, long defaultValue) {
        Long l = TypeConvert.convert(Long.class, get(property));;
        return l == null ? defaultValue : l;
    }

    @Override
    public Integer getInteger(String property) {
        return TypeConvert.convert(Integer.class, get(property));
    }

    @Override
    public int getInt(String property) {
        return getInt(property, 0);
    }

    @Override
    public int getInt(String property, int defaultValue) {
        Integer i = getInteger(property);
        return i == null ? defaultValue : i;
    }

}
